package cn.smbms.service.user;

import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */

@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource
	private UserMapper userMapper;

	@Override
	public int add(User user) {
		return userMapper.addUserInfo(user);
	}


	@Override
	public User login(String userCode, String userPassword) {
		User user=userMapper.getUserByUCode(userCode);

		if(!user.getUserPassword().equals(userPassword)){
			user=null;
		}

		return user;
	}

	@Override
	public List<Object> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
		List<Object> userList = null;
		try {

			Integer maxIndex=currentPageNo*pageSize;
			Integer startIndex=maxIndex-pageSize;
			userList = userMapper.getUserList(queryUserName,queryUserRole,startIndex,maxIndex);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return userList;
	}

	@Override
	public boolean deleteUserById(Integer delId) {
		boolean flag = false;

		try {
			if(userMapper.deleteUserById(delId) > 0)
				flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return flag;
	}

	@Override
	public User getUserById(String id) {
		return userMapper.getUserById(id);
	}

	@Override
	public boolean modify(User user) {
		boolean flag = false;

		try {
			if(userMapper.modify(user) > 0)
				flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return flag;
	}
	@Override
	public boolean updatePwd(int id, String pwd) {
		boolean flag = false;

		try{
			if(userMapper.updatePwd(id,pwd) > 0)
				flag = true;
		}catch (Exception e) {
			e.printStackTrace();
		}

		return flag;
	}
	@Override
	public int getUserCount(String queryUserName, int queryUserRole) {
		int count = 0;

		try {
			count = userMapper.getUserCount(queryUserName,queryUserRole);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return count;
	}

	@Override
	public User selectUserCodeExist(String userCode) {
		return userMapper.getUserByUCode(userCode);
	}

}
